/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import init.Config;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Music.findAll", query = "SELECT m FROM Music m ORDER BY m.id ASC"),
    @NamedQuery(name = "Music.findById", query = "SELECT m FROM Music m WHERE m.id=:id"),
    @NamedQuery(name = "Music.findByGenre", query = "SELECT m FROM Music m WHERE m.genre=:genre")})
public class Music implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String coverImage;
    private String title;
    @ManyToOne
    private Artist artist;
    @ManyToOne
    private Album album;
    @ManyToOne
    private Genre genre;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date releaseDate; // the year of the album is the year of the release date
    private int totalTime; // in secondes
    private String filePath;
    private int nbTimesPlayed;

    // 
    // Constructors
    //
    public Music() {
    }
    
    public Music(String title, Artist artist, Genre genre, Date releaseDate, int totalTime, String filePath) {
        this.coverImage = Config.DEFAULT_COVER_ART_FILE.replaceAll("\\\\", "/");
        this.title = title;
        this.artist = artist;
        this.genre = genre;
        this.releaseDate = releaseDate;
        this.totalTime = totalTime;
        this.filePath = filePath;
        
        this.nbTimesPlayed = 0;
    }
    
    //
    // Accessors
    //
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCoverImage() {
        return coverImage;
    }

    public void setCoverImage(String coverImage) {
        this.coverImage = coverImage;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Artist getArtist() {
        return artist;
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }
    
    public Genre getGenre() {
        return genre;
    }

    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public int getNbTimesPlayed() {
        return nbTimesPlayed;
    }

    public void setNbTimesPlayed(int nbTimesPlayed) {
        this.nbTimesPlayed = nbTimesPlayed;
    }

    public String getTotalTimeStr() {
        String minutes = String.valueOf(totalTime /60);
        String secondes = String.valueOf(totalTime%60);
        
        // Format
        if(minutes.length() < 2) {
            minutes = "0" + minutes;
        }
        if(secondes.length() < 2) {
            secondes = "0" + secondes;
        }
        
        return minutes + ":" + secondes;
    }
    
    public void incPlayCount() {
        nbTimesPlayed++;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Music)) {
            return false;
        }
        Music other = (Music) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Music[ id=" + id + " ]";
    }
}
